/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogFrame.java
 *
 * Created on 12-ene-2012, 11:35:28
 */
package occho.view;

import java.awt.Color;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import occho.model.FachadaAdministrador;
import occho.model.Log;
import occho.view.resources.Messages;

/**
 *
 * @author inftel
 */
public class LogFrame extends javax.swing.JInternalFrame implements Internacionalizable {

    private OcchoView view;
    private List<Log> listaLogs;
    private FachadaAdministrador fachada;

    /** Creates new form LogFrame
     * @param view 
     */
    public LogFrame(OcchoView view) {
        initComponents();
        try {
            this.view = view;
            fachada = new FachadaAdministrador(view.getUsuario());
            listaLogs = fachada.getListaLogs();

            if (!listaLogs.isEmpty()) {
                int i = 0;
                for (Log l : listaLogs) {
                    if (i < tablaLogs.getRowCount()) {
                        tablaLogs.setValueAt(l.getUsuario().getEmail(), i, 0);
                        tablaLogs.setValueAt(l.getFecha(), i, 1);
                        tablaLogs.setValueAt(l.getAccion(), i, 2);
                    }
                    i++;
                }
            }
            setLenguaje();
        } catch (ClassNotFoundException ex) {
        } catch (SQLException ex) {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaLogs = new javax.swing.JTable();
        btnresetLog = new javax.swing.JButton();

        setClosable(true);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaLogs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "E-mail", "Fecha", "Accion"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaLogs.setName("tablaLogs"); // NOI18N
        jScrollPane1.setViewportView(tablaLogs);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(LogFrame.class);
        tablaLogs.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tablaLogs.columnModel.title0")); // NOI18N
        tablaLogs.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tablaLogs.columnModel.title1")); // NOI18N
        tablaLogs.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tablaLogs.columnModel.title2")); // NOI18N

        btnresetLog.setText(resourceMap.getString("btnresetLog.text")); // NOI18N
        btnresetLog.setName("btnresetLog"); // NOI18N
        btnresetLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnresetLogActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(459, Short.MAX_VALUE)
                        .addComponent(btnresetLog)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnresetLog)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnresetLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnresetLogActionPerformed
        tablaLogs.getRowCount();
        DefaultTableModel model = (DefaultTableModel) tablaLogs.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }

        tablaLogs = new JTable(model);
        tablaLogs.repaint();

        try {
            if (fachada.resetLogs()) {
                view.setStatusMessageLabel("Logs borrados correctamente", Color.BLACK);
            } else {
                view.setStatusMessageLabel("Error al borrar los logs", Color.red);
            }
        } catch (SQLException ex) {
        } catch (ClassNotFoundException ex) {
        }

    }//GEN-LAST:event_btnresetLogActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnresetLog;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaLogs;
    // End of variables declaration//GEN-END:variables

    /**
     * Establece lenguaje
     */
    public void setLenguaje() {
        setTitle(Messages.getString("Form.title", Messages.BUNDLE_NAME_LOG));
        btnresetLog.setText(Messages.getString("btnresetLog.text", Messages.BUNDLE_NAME_LOG));
        tablaLogs.getColumnModel().getColumn(0).setHeaderValue(Messages.getString("tablaLogs.columnModel.title0", Messages.BUNDLE_NAME_LOG));
        tablaLogs.getColumnModel().getColumn(1).setHeaderValue(Messages.getString("tablaLogs.columnModel.title1", Messages.BUNDLE_NAME_LOG));
        tablaLogs.getColumnModel().getColumn(2).setHeaderValue(Messages.getString("tablaLogs.columnModel.title2", Messages.BUNDLE_NAME_LOG));
        pack();
    }
}
